package com.qcs.ando.memory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.qcs.ando.BaseActivity;
import com.qcs.ando.R;
import com.qcs.ando.manage.ContextManager;
import com.qcs.ando.manage.ImageManager;

public class MainActivity extends BaseActivity {
    private static final int FINISH_LOAD_IMG = 2000;
    private static final int LOAD_IMG_FAIL = 2001;

    private static final String[] hpngs = {
            "memory_hpng/artist_bg.png",
            "memory_hpng/background02.png",
            "memory_hpng/background03.png",
            "memory_hpng/bg01.png",
            "memory_hpng/bg02.png",
            "memory_hpng/bg03.png",
            "memory_hpng/bg04.png",
            "memory_hpng/big_map_bg.png",
            "memory_hpng/cankao.png",
            "memory_hpng/cartoon_bg.png",
            "memory_hpng/daily_course_bg.png",
            "memory_hpng/map_island_main.png",
            "memory_hpng/memory_test_1.png",
            "memory_hpng/science_bg.png",
            "memory_hpng/society_bg.png",
            "memory_hpng/speak_bg.png",
            "memory_hpng/square_bg.png",
            "memory_hpng/test_background.png",
            "memory_hpng/wel_background.png",
            "memory_hpng/cloud4.png"
    };

    private static final String[] ljpgs = {
            "memory_ljpg/artist_bg.png",
            "memory_ljpg/background02.png",
            "memory_ljpg/background03.png",
            "memory_ljpg/bg01.png",
            "memory_ljpg/bg02.png",
            "memory_ljpg/bg03.png",
            "memory_ljpg/bg04.png",
            "memory_ljpg/big_map_bg.png",
            "memory_ljpg/cankao.png",
            "memory_ljpg/cartoon_bg.png",
            "memory_ljpg/daily_course_bg.png",
            "memory_ljpg/map_island_main.png",
            "memory_ljpg/memory_test_1.png",
            "memory_ljpg/science_bg.png",
            "memory_ljpg/society_bg.png",
            "memory_ljpg/speak_bg.png",
            "memory_ljpg/square_bg.png",
            "memory_ljpg/test_background.png",
            "memory_ljpg/wel_background.png",
            "memory_hpng/cloud4.png"
    };

    private static final int[] ress = {
    //            R.drawable.artist_bg,
//            R.drawable.background02,
//            R.drawable.background03,
//            R.drawable.bg01,
//            R.drawable.bg02,
    //            R.drawable.bg03,
//            R.drawable.bg04,
//            R.drawable.big_map_bg,
//            R.drawable.cankao,
//            R.drawable.cartoon_bg,
//            R.drawable.daily_course_bg,
//            R.drawable.map_island_main,
//            R.drawable.memory_test_1,
//            R.drawable.science_bg,
//            R.drawable.society_bg,
//            R.drawable.speak_bg,
//            R.drawable.square_bg,
//            R.drawable.test_background,
//            R.drawable.wel_background
    };
    private List<Bitmap> bitmapList;
    private String[] imgs;

    private Handler mHandler;

    private ImageManager im;
    private ContextManager cm;

    private ImageView memoryMainImg;
    private ImageView memoryMainImg1;

    private Button memoryMainBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_main_lay);

        memoryMainImg = (ImageView) findViewById(R.id.memoryMainImg);
        memoryMainImg1 = (ImageView) findViewById(R.id.memoryMainImg1);

        memoryMainBtn = (Button) findViewById(R.id.memoryMainBtn);

        init();
    }

    private void init() {
        imgs = ljpgs;
        bitmapList = new ArrayList<Bitmap>();
        mHandler = new OutHandler();

        im = new ImageManager(this);
        cm = new ContextManager(this);

        memoryMainBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                cm.goToOtherActivity(SurfaceActivity.class, null, true);
            }
        });
        memoryMainBtn.setEnabled(false);
    }

    @Override
    protected void onResume() {
        super.onResume();

        startLoadAssetsImageByOptionThread();
    }

    @Override
    protected void onPause() {
        recycle();

        super.onPause();
    }

    @Override
    protected void execute(Bundle bund) {
//        recycle();

        super.execute(bund);
    }

    private void finishLoadImg() {
        memoryMainImg
                .setImageBitmap(bitmapList.get(bitmapList.size() - 2));
        memoryMainImg1
                .setImageBitmap(bitmapList.get(bitmapList.size() - 1));

        memoryMainBtn.setEnabled(true);

        Toast.makeText(MainActivity.this, "finish load all image.", 0)
                .show();
        Log.i(TAG, "finish load all image.");
    }

    private void recycle() {
        for (Bitmap bitmap : bitmapList) {
            if (null != bitmap) {
                bitmap.recycle();
                bitmap = null;
            }
        }

        bitmapList.clear();
        call(SurfaceActivity.class.getName());
    }

    void startLoadAssetsImageThread() {
        LoadAssetsImageThread t1 = new LoadAssetsImageThread();

        t1.setName("t1");
        t1.start();
    }

    void startLoadResDrawableThread() {
        LoadResDrawableThread t2 = new LoadResDrawableThread();
        t2.setName("t2");
        t2.start();
    }

    void startLoadAssetsImageByOptionThread() {
        LoadAssetsImageByOptionThread t3 = new LoadAssetsImageByOptionThread();
        t3.setName("t3");
        t3.start();
    }

    void startLoadAssetsImageByOptionMatrixThread() {
        LoadAssetsImageByOptionMatrixThread t4 = new LoadAssetsImageByOptionMatrixThread();
        t4.setName("t4");
        t4.start();
    }

    /** ����һ������wath��obj��Message */
    private Message createMessage(int wath, String img) {
        Message msg = new Message();
        msg.what = wath;
        msg.obj = img;
        return msg;
    }

    private void loadAssetsImage() {
        for (int i = 0; i < imgs.length; i++) {
            try {
//                    Bitmap bitmap = null;
                Bitmap bitmap = im.createBitmap4Assets(imgs[i]);
                bitmapList.add(bitmap);

                Log.i(TAG, "load " + imgs[i] + " and at " + i + " index.");
            } catch (IOException e) {
                Message msg = createMessage(LOAD_IMG_FAIL, imgs[i]);

                mHandler.sendMessage(msg);
            }
        }
        mHandler.sendEmptyMessage(FINISH_LOAD_IMG);
    }

    private void loadResDrawable() {
        for (int i = 0; i < ress.length; i++) {
            try {
//                    Bitmap bitmap = null;
                Bitmap bitmap = im.createBitmap4Res(ress[i]);
                bitmapList.add(bitmap);

                Log.i(TAG, "load " + ress[i] + " and at " + i + " index.");
            } catch (IOException e) {
                Message msg = createMessage(LOAD_IMG_FAIL,
                        String.valueOf(ress[i]));

                mHandler.sendMessage(msg);
            }
        }
        mHandler.sendEmptyMessage(FINISH_LOAD_IMG);
    }

    private void loadAssetsImageByOptions() {
        for (int i = 0; i < imgs.length; i++) {
            try {
//                    Bitmap bitmap = null;
                Bitmap bitmap = im
                        .createBitmap4AssetsByOption(imgs[i]);
                bitmapList.add(bitmap);

                Log.i(TAG, "load " + imgs[i] + " and at " + i + " index.");
            } catch (IOException e) {
                Message msg = createMessage(LOAD_IMG_FAIL, imgs[i]);

                mHandler.sendMessage(msg);
            }
        }
        mHandler.sendEmptyMessage(FINISH_LOAD_IMG);
    }

    private void loadAssetsImageByOptionsMatrix() {
        for (int i = 0; i < imgs.length; i++) {
            try {
//                    Bitmap bitmap = null;
                Bitmap bitmap = im
                        .createBitmap4AssetsByOptionMatrix(imgs[i], 2);
                bitmapList.add(bitmap);

                Log.i(TAG, "load " + imgs[i] + " and at " + i + " index.");
            } catch (IOException e) {
                Message msg = createMessage(LOAD_IMG_FAIL, imgs[i]);

                mHandler.sendMessage(msg);
            }
        }
        mHandler.sendEmptyMessage(FINISH_LOAD_IMG);
    }

    class OutHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case FINISH_LOAD_IMG:
                finishLoadImg();
                break;
            case LOAD_IMG_FAIL:
                Log.e(TAG, "load " + String.valueOf(msg.obj) + " fail.");
                break;

            default:
                break;
            }
            super.handleMessage(msg);
        }
    }

    class LoadAssetsImageThread extends Thread {
        @Override
        public void run() {
            Log.i(TAG, this.getName() + " thread run.");
            loadAssetsImage();
        }
    }

    class LoadResDrawableThread extends Thread {
        @Override
        public void run() {
            Log.i(TAG, this.getName() + " thread run.");
            loadResDrawable();
        }
    }

    class LoadAssetsImageByOptionThread extends Thread {
        @Override
        public void run() {
            Log.i(TAG, this.getName() + " thread run.");
            loadAssetsImageByOptions();
        }
    }

    class LoadAssetsImageByOptionMatrixThread extends Thread {
        @Override
        public void run() {
            Log.i(TAG, this.getName() + " thread run.");
            loadAssetsImageByOptionsMatrix();
        }
    }

}
